package com.ecreditpal.audio.platform.xunfei;

import com.alibaba.fastjson.JSON;
import com.iflytek.msp.cpdb.lfasr.client.LfasrClientImp;
import com.iflytek.msp.cpdb.lfasr.exception.LfasrException;
import com.iflytek.msp.cpdb.lfasr.model.LfasrType;
import com.iflytek.msp.cpdb.lfasr.model.Message;
import com.iflytek.msp.cpdb.lfasr.model.ProgressStatus;
import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;

/**
 * Created by lishunyi on 2018/6/15
 *
 * 讯飞语音转文字Demo。
 * 结果获取方式为异步获取。当前服务器可能会存在排队情况，获取结果的间隔时间会存在不确定的情况（一般15～30分钟），最长间隔时间为24小时。
 * 
 */
@Slf4j
public class XunFeiDemo {
    private static final String local_file = "/Users/lishunyi/Desktop/audio/pp2.wav";
    /*
     LfasrType枚举类型说明：

     16K标准版-已录制音频，支持格式wav,flac,opus,mp3,m4a
     LfasrType.LFASR_STANDARD_RECORDED_AUDIO

     8K电话专用版-已录制音频，支持格式wav,flac,mp3
     LfasrType.LFASR_TELEPHONY_RECORDED_AUDIO
     Demo先使用免费的服务（时长5小时，周期为1个月）
     */
    private static final LfasrType type = LfasrType.LFASR_TELEPHONY_RECORDED_AUDIO;
    private static int sleepSecond = 1;
    public static void main(String[] args) {
        LfasrClientImp lc = null;
        try {
            lc = LfasrClientImp.initLfasrClient();
        } catch (LfasrException e) {
            Message initMsg = JSON.parseObject(e.getMessage(), Message.class);
            log.info("ecode=" + initMsg.getErr_no());
            log.info("failed=" + initMsg.getFailed());
        }

        String task_id = "";
        HashMap<String, String> params = new HashMap<String, String>();
        params.put("has_participle", "true");
        try {
            Message uploadMsg = lc.lfasrUpload(local_file, type, params);

            int ok = uploadMsg.getOk();
            if (ok == 0) {
                task_id = uploadMsg.getData();
                log.info("task_id=" + task_id);
            } else {
                log.info("ecode=" + uploadMsg.getErr_no());
                log.info("failed=" + uploadMsg.getFailed());
            }
        } catch (LfasrException e) {
            Message uploadMsg = JSON.parseObject(e.getMessage(), Message.class);
            log.info("ecode=" + uploadMsg.getErr_no());
            log.info("failed=" + uploadMsg.getFailed());
        }

        while (true) {
            try {
                Thread.sleep(sleepSecond * 1000);
                log.info("waiting ...");
            }catch(InterruptedException e) {
            }
            try {
                Message progressMsg = lc.lfasrGetProgress(task_id);

                if (progressMsg.getOk() != 0) {
                    log.info("task was fail. task_id:" + task_id);
                    log.info("ecode=" + progressMsg.getErr_no());
                    log.info("failed=" + progressMsg.getFailed());

                    continue;
                } else {
                    ProgressStatus progressStatus = JSON.parseObject(progressMsg.getData(), ProgressStatus.class);
                    if (progressStatus.getStatus() == 9) {
                        log.info("task was completed. task_id:" + task_id);
                        break;
                    } else {
                        log.info("task was incomplete. task_id:" + task_id + ", status:" + progressStatus.getDesc());
                        continue;
                    }
                }
            } catch (LfasrException e) {
                Message progressMsg = JSON.parseObject(e.getMessage(), Message.class);
                log.info("ecode=" + progressMsg.getErr_no());
                log.info("failed=" + progressMsg.getFailed());
            }
        }

        try {
            Message resultMsg = lc.lfasrGetResult(task_id);
//            logger.info(resultMsg.getData());
            if (resultMsg.getOk() == 0) {
                log.info("[Message]Result: {}", resultMsg.getData());
            } else {
                log.info("ecode=" + resultMsg.getErr_no());
                log.info("failed=" + resultMsg.getFailed());
            }
        } catch (LfasrException e) {
            Message resultMsg = JSON.parseObject(e.getMessage(), Message.class);
            log.info("ecode=" + resultMsg.getErr_no());
            log.info("failed=" + resultMsg.getFailed());
        }
    }


}
